package com.service.manage.imp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.service.manage.DatabaseService;
import com.util.MySQLDatabaseBackup;
import com.util.StringFinal;
import com.util.ZipUtils;
@Service
public class DatabaseServiceImp implements DatabaseService{

    @Override
    public int exportDB(String pathway) {
	String[] strs=pathway.split(File.separator.equals("\\")?"\\\\":"/");
	//备份数据库
	String savePath = strs[0]+File.separator+StringFinal.backuppath;//备份的目的地址
	Date date = new Date(); 
	SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); 
	int check = MySQLDatabaseBackup.exportDatabaseTool(StringFinal.ip, StringFinal.username, StringFinal.password, savePath, df.format(date)+".sql", StringFinal.database);
	//压缩文件
	FileOutputStream fos1;
	FileOutputStream fos2;
	try {
	    fos1 = new FileOutputStream(new File(savePath+File.separator+df.format(date)+".zip"));
	    ZipUtils.toZip(pathway+File.separator+StringFinal.path, fos1,true);
	    fos2 = new FileOutputStream(new File(savePath+File.separator+df.format(date)+"view.zip"));
	    ZipUtils.toZip(pathway+File.separator+StringFinal.viewpath, fos2,true);
	} catch (FileNotFoundException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return check;
    }
      
}
